package com.xc.service.impl;


import com.xc.mapper.DeptMapper;
import com.xc.pojo.Dept;
import com.xc.service.DeptService;
import com.xc.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpService empService;

    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll();
    }

    @Override
    public Dept selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public int deleteById(Integer id) {
        //删除部门，同时删除部门对应的员工
        //不要直接操作EmpMapper，还是建议EmpService操作Emp的内容，分开
        //所以依赖注入EmpService，操作员工表
        //service操作mapper层
        //同时添加事务，防止一个表删除完了，另一个表数据还在。建议加在Service上面，实现类就都有了
        empService.deleteEmpByDeptId(id);
        //特意写的错误，验证事务
        int a = 2 /0;
        return deptMapper.deleteById(id);
    }

    @Override
    public int insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }
}
